package com.fwh.gmall.service.impl;

import com.fwh.gmall.dao.ElectricDao;
import com.fwh.gmall.dto.ElectricData;
import com.fwh.gmall.dto.ElectricQueryDTO;
import com.fwh.gmall.dto.ElectricResponse;
import com.fwh.gmall.service.ElectricService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class ElectricServiceImpl implements ElectricService {

    @Autowired
    private ElectricDao electricDao;



    /**
     * 查询电量数据
     * @param queryDTO 查询请求参数
     * @return 响应结果
     */
    @Override
    public ElectricResponse queryElectricData(ElectricQueryDTO queryDTO) throws IllegalAccessException {
        // 查询原始数据
        List<ElectricData> dataList = electricDao.queryElectricData(
                queryDTO.getType(),
                queryDTO.getStartDate(),
                queryDTO.getHomeGroupId()
        );

        return convertToResponse(dataList);
    }
    /**
     * 将原始数据转换为响应格式
     */
    private ElectricResponse convertToResponse(List<ElectricData> dataList) {
        ElectricResponse response = new ElectricResponse();

        List<String> deviceTimeArray = new ArrayList<>();
        List<Double> kwhSunArray = new ArrayList<>();
        List<Double> kwhGenArray = new ArrayList<>();
        List<Double> kwhUtilnArray = new ArrayList<>();
        List<Double> kwhUtiOutArray = new ArrayList<>();
        List<Double> kwhFhpChgArray = new ArrayList<>();
        List<Double> kwhFhpDiArray = new ArrayList<>();
        List<Double> kwhLoadArray = new ArrayList<>();
        List<Double> kwhGridLoadArray = new ArrayList<>();
        List<Double> kwhSolarLoadArray = new ArrayList<>();
        List<Double> kwhFhpLoadArray = new ArrayList<>();
        List<Double> kwhGenLoadArray = new ArrayList<>();
        List<Double> gridChBatArray = new ArrayList<>();
        List<Double> batOutGridArray = new ArrayList<>();
        List<Double> genChBatArray = new ArrayList<>();
        List<Double> soChBatArray = new ArrayList<>();
        List<Double> soOutGridArray = new ArrayList<>();
        List<Double> proximalSolarWhArray = new ArrayList<>();
        List<Double> mpptWhArray = new ArrayList<>();
        List<Double> remoteSolarWhTotalArray = new ArrayList<>();
        List<Double> meterkitPvWhArray = new ArrayList<>();
        List<Double> secondaryPvWhArray = new ArrayList<>();
        List<Double> mpanPv1Wh = new ArrayList<>();
        List<Double> mpanPv2Wh = new ArrayList<>();
        List<Double> apbox20PvWh = new ArrayList<>();

        for (ElectricData data : dataList) {
            deviceTimeArray.add(data.getDayTime());
            kwhSunArray.add(convertToDouble(data.getKwhSun()));
            kwhGenArray.add(convertToDouble(data.getKwhGen()));
            kwhUtilnArray.add(convertToDouble(data.getKwhUtiIn()));
            kwhUtiOutArray.add(convertToDouble(data.getKwhUtiOut()));
            kwhFhpChgArray.add(convertToDouble(data.getKwhFhpChg()));
            kwhFhpDiArray.add(convertToDouble(data.getKwhFhpDi()));
            kwhLoadArray.add(convertToDouble(data.getKwhLoad()));
            kwhGridLoadArray.add(convertToDouble(data.getKwhGridLoad()));
            kwhSolarLoadArray.add(convertToDouble(data.getKwhSolarLoad()));
            kwhFhpLoadArray.add(convertToDouble(data.getKwhFhpLoad()));
            kwhGenLoadArray.add(convertToDouble(data.getKwhGenLoad()));
            gridChBatArray.add(convertToDouble(data.getGridChBat()));
            batOutGridArray.add(convertToDouble(data.getBatOutGrid()));
            genChBatArray.add(convertToDouble(data.getGenChBat()));
            soChBatArray.add(convertToDouble(data.getSoChBat()));
            soOutGridArray.add(convertToDouble(data.getSoOutGrid()));
            proximalSolarWhArray.add(convertToDouble(data.getProximalSolarWh()));
            mpptWhArray.add(convertToDouble(data.getMpptWh()));

            // 计算远程太阳能总计
            double remoteSolarTotal = convertToDouble(data.getRemoteSolar1Wh()) +
                    convertToDouble(data.getRemoteSolar2Wh()) +
                    convertToDouble(data.getRemoteSolar3Wh());
            remoteSolarWhTotalArray.add(remoteSolarTotal);

            meterkitPvWhArray.add(convertToDouble(data.getMeterkitPvWh()));
            secondaryPvWhArray.add(convertToDouble(data.getSecondaryPvWh()));
            mpanPv1Wh.add(convertToDouble(data.getMpanPv1Wh()));
            mpanPv2Wh.add(convertToDouble(data.getMpanPv2Wh()));
            apbox20PvWh.add(convertToDouble(data.getApbox20PvWh()));
        }

        response.setDeviceTimeArray(deviceTimeArray);
        response.setKwhSunArray(kwhSunArray);
        response.setKwhGenArray(kwhGenArray);
        response.setKwhUtilnArray(kwhUtilnArray);
        response.setKwhUtiOutArray(kwhUtiOutArray);
        response.setKwhFhpChgArray(kwhFhpChgArray);
        response.setKwhFhpDiArray(kwhFhpDiArray);
        response.setKwhLoadArray(kwhLoadArray);
        response.setKwhGridLoadArray(kwhGridLoadArray);
        response.setKwhSolarLoadArray(kwhSolarLoadArray);
        response.setKwhFhpLoadArray(kwhFhpLoadArray);
        response.setKwhGenLoadArray(kwhGenLoadArray);
        response.setGridChBatArray(gridChBatArray);
        response.setBatOutGridArray(batOutGridArray);
        response.setGenChBatArray(genChBatArray);
        response.setSoChBatArray(soChBatArray);
        response.setSoOutGridArray(soOutGridArray);
        response.setProximalSolarWhArray(proximalSolarWhArray);
        response.setMpptWhArray(mpptWhArray);
        response.setRemoteSolarWhTotalArray(remoteSolarWhTotalArray);
        response.setMeterkitPvWhArray(meterkitPvWhArray);
        response.setSecondaryPvWhArray(secondaryPvWhArray);
        response.setMpanPv1Wh(mpanPv1Wh);
        response.setMpanPv2Wh(mpanPv2Wh);
        response.setApbox20PvWh(apbox20PvWh);

        return response;
    }

    /**
     * 将BigDecimal转换为Double，处理null值
     */
    private Double convertToDouble(BigDecimal value) {
        return value != null ? value.doubleValue() : 0.0;
    }



}
